//
//  KYTabBarController.swift
//  NormalHE
//
//  Created by SHANGLU on 16/12/29.
//  Copyright © 2016年 heky. All rights reserved.
//

import UIKit


class KYTabBarController: UITabBarController {
    weak private(set) var customTabBar:KYTabBar!
    weak private(set) var home:KYHomeViewController!
    weak private(set) var community:KYCommunityViewController!
    weak private(set) var discover:KYDiscoverViewController!
    weak private(set) var mine:KYMineViewController!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setUpTabBar()
        setUpAllChildControllers()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        self.tabBar.subviews.forEach { (subView) in
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subView.removeFromSuperview()
            }
        }
    }
    
    func setUpTabBar(){
    
        let customBar = KYTabBar(frame: self.tabBar.bounds)
        self.tabBar.addSubview(customBar)
        self.customTabBar = customBar
    }
    
    func setUpAllChildControllers(){
        let home = KYHomeViewController(nibName: "KYHomeViewController", bundle: nil)
        setupChildController(home, title: "首页", normalImage: "unselected_home", selectedImage: "selected_home")
        self.home = home
        let discover = KYDiscoverViewController(nibName: "KYDiscoverViewController", bundle: nil)
         setupChildController(discover, title: "发现", normalImage: "unselected_find", selectedImage: "selected_find")
        self.discover = discover
        let community = KYCommunityViewController(nibName: "KYCommunityViewController", bundle: nil)
        setupChildController(community, title: "社区", normalImage: "unselected_shequ", selectedImage: "selected_shequ")
        self.community = community
        let mine = KYMineViewController(nibName: "KYMineViewController", bundle: nil)
        setupChildController(community, title: "我的", normalImage: "unselected_mine", selectedImage: "selected_mine")
        mine.tabBarItem.badgeValue = "10"
        self.mine = mine
    
    }
    
    func setupChildController(childCtrl:UIViewController,title:String,normalImage:String,selectedImage:String){
    
        childCtrl.title = title
        childCtrl.tabBarItem.image = UIImage(named: normalImage)
        childCtrl.tabBarItem.selectedImage = UIImage(named: selectedImage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let navigationCtrl = KYBaseNavigationViewController(rootViewController: childCtrl)
        self.addChildViewController(navigationCtrl)
        self.customTabBar.addTabBarButton(childCtrl.tabBarItem)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
